﻿//
// Created by francklinson on 2021/3/17.
//

#include <iostream>
void swapr(int & a, int & b); // a, b are aliases for ints
void swapp(int * p, int * q); // p, q are addresses of ints
void swapv(int a, int b);     // a, b are new variables
int main()
{
    using namespace std;
    int wallet1 = 300;
    int wallet2 = 350;
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using references to swap contents:\n";
    swapr(wallet1, wallet2);  // pass variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using pointers to swap contents again:\n";
    swapp(&wallet1, &wallet2);// pass addresses of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Trying to use passing by value:\n";
    swapv(wallet1, wallet2);  // pass values of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;
    return 0;
}

void swapr(int & a, int & b)  // use references 引用
{
    int temp;

    temp = a;                 // use a, b for values of variables
    a = b;
    b = temp;
}

void swapp(int * p, int * q) // use pointers 指针
{
    int temp;

    temp = *p;               // use *p, *q for values of variables
    *p = *q;
    *q = temp;
}

void swapv(int a, int b)     // try using values 值传递
{
    int temp;

    temp = a;                // use a, b for values of variables
    a = b;
    b = temp;
}